package com.lfk.justweengine.utils.tool;

import com.lfk.justweengine.ResourceTable;
import com.lfk.justweengine.utils.tools.PicUtils;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.ability.delegation.AbilityDelegatorRegistry;
import ohos.agp.components.Image;
import ohos.agp.components.ScrollView;
import ohos.agp.components.element.Element;
import ohos.agp.components.element.PixelMapElement;
import ohos.app.Context;
import ohos.global.resource.RawFileEntry;
import ohos.global.resource.Resource;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;
import ohos.media.image.common.PixelFormat;
import org.junit.Assert;
import org.junit.Test;

import java.io.IOException;
import java.util.List;


public class PicUtilsOhosTest {

    private final String iconFilePath = "resources/rawfile/graphic/icon.png";

    @Test
    public void testBitmapToString() {
        Context context = AbilityDelegatorRegistry.getAbilityDelegator().getAppContext();
        PixelMap iconPiexMap = getPixelMapFormFilePath(context, iconFilePath);
        String str = PicUtils.bitmapToString(iconPiexMap);
        Assert.assertTrue(str.length() > 1);
    }

    @Test
    public void testStringToBitmap() {
        Context context = AbilityDelegatorRegistry.getAbilityDelegator().getAppContext();
        PixelMap iconPiexMap = getPixelMapFormFilePath(context, iconFilePath);
        String str = PicUtils.bitmapToString(iconPiexMap);
        PixelMap pixelMap = PicUtils.stringToBitmap(str);
        Assert.assertTrue(pixelMap.getImageInfo().size.width > 0);
    }

    @Test
    public void testPrintAllScreen() {
        Ability context = AbilityDelegatorRegistry.getAbilityDelegator().getCurrentTopAbility();
        PicUtils pp = new PicUtils();
        PixelMap pixelMap = pp.printAllScreen(context);
        Assert.assertNull(pixelMap);
    }

    @Test
    public void testGetBitmapFromView() {
        Context context = AbilityDelegatorRegistry.getAbilityDelegator().getAppContext();
        Image image = new Image(context);
        image.setPixelMap(ResourceTable.Media_icon);
        image.setWidth(100);
        image.setHeight(300);
        PixelMap pixelMap = PicUtils.getBitmapFromView(image);
        Assert.assertTrue(pixelMap.getImageInfo().size.width > 0);
    }

    @Test
    public void testCompressImage() {
        Context context = AbilityDelegatorRegistry.getAbilityDelegator().getAppContext();
        PixelMap iconPiexMap = getPixelMapFormFilePath(context, iconFilePath);
        PixelMap pixelMap = PicUtils.compressImage(iconPiexMap);
        Assert.assertTrue(pixelMap.getImageInfo().size.width > 0);
    }

    @Test
    public void testTestCompressImage() {
        Context context = AbilityDelegatorRegistry.getAbilityDelegator().getAppContext();
        PixelMap iconPiexMap = getPixelMapFormFilePath(context, iconFilePath);
        PixelMap pixelMap = PicUtils.compressImage(iconPiexMap, 50);
        Assert.assertTrue(pixelMap.getImageInfo().size.width > 0);
    }

    @Test
    public void testCompressWithMeasure() {
        Context context = AbilityDelegatorRegistry.getAbilityDelegator().getAppContext();
        PixelMap iconPiexMap = getPixelMapFormFilePath(context, iconFilePath);
        PixelMap pixelMap = PicUtils.compressWithMeasure(iconPiexMap);
        Assert.assertTrue(pixelMap.getImageInfo().size.width > 0);
    }

    @Test
    public void testGetHttpBitmap() {
        PixelMap pixelMap = PicUtils.getHttpBitmap("https://www.html5tricks.com/wp-content/uploads/2021/04/html5-canvas-photo-tear.png");
        Assert.assertTrue(pixelMap.getImageInfo().size.width > 0);
    }

    @Test
    public void testGetBitmapFromResources() {
        Context context = AbilityDelegatorRegistry.getAbilityDelegator().getAppContext();
        PixelMap pixelMap = PicUtils.getBitmapFromResources(context, ResourceTable.Media_icon);
        Assert.assertTrue(pixelMap.getImageInfo().size.width > 0);
    }

    @Test
    public void testBitmap2Bytes() {
        Context context = AbilityDelegatorRegistry.getAbilityDelegator().getAppContext();
        PixelMap iconPiexMap = getPixelMapFormFilePath(context, iconFilePath);
        PicUtils pp = new PicUtils();
        byte[] piexMapBytes = pp.Bitmap2Bytes(iconPiexMap);
        Assert.assertTrue(piexMapBytes.length > 0);
    }

    @Test
    public void testBytes2Bimap() {
        Context context = AbilityDelegatorRegistry.getAbilityDelegator().getAppContext();
        PixelMap iconPiexMap = getPixelMapFormFilePath(context, iconFilePath);
        PicUtils pp = new PicUtils();
        byte[] piexMapBytes = pp.Bitmap2Bytes(iconPiexMap);
        PixelMap pixelMap = pp.Bytes2Bimap(piexMapBytes);
        Assert.assertTrue(pixelMap.getImageInfo().size.width > 0);
    }

    @Test
    public void testZoomBitmap() {
        Context context = AbilityDelegatorRegistry.getAbilityDelegator().getAppContext();
        PixelMap iconPiexMap = getPixelMapFormFilePath(context, iconFilePath);
        PixelMap pixelMap = PicUtils.zoomBitmap(iconPiexMap, 1000, 2000);
        Assert.assertTrue(pixelMap.getImageInfo().size.width > 0);
    }

    @Test
    public void testDrawable2Bitmap() {
        Context context = AbilityDelegatorRegistry.getAbilityDelegator().getAppContext();
        PixelMap iconPiexMap = getPixelMapFormFilePath(context, iconFilePath);
        Element element = new PixelMapElement(iconPiexMap);
        PixelMap pixelMap = PicUtils.drawable2Bitmap(element);
        Assert.assertTrue(pixelMap.getImageInfo().size.width > 0);
    }

    @Test
    public void testBitmap2Drawable() {
        Context context = AbilityDelegatorRegistry.getAbilityDelegator().getAppContext();
        PixelMap iconPiexMap = getPixelMapFormFilePath(context, iconFilePath);
        Element element = PicUtils.Bitmap2Drawable(iconPiexMap);
        Assert.assertNotNull(element);
    }

    @Test
    public void testGetRoundedCornerBitmap() {
        Context context = AbilityDelegatorRegistry.getAbilityDelegator().getAppContext();
        PixelMap iconPiexMap = getPixelMapFormFilePath(context, iconFilePath);
        PixelMap pixelMap = PicUtils.getRoundedCornerBitmap(iconPiexMap, 100);
        Assert.assertTrue(pixelMap.getImageInfo().size.width > 0);
    }

    @Test
    public void testCreateReflectionImageWithOrigin() {
        Context context = AbilityDelegatorRegistry.getAbilityDelegator().getAppContext();
        PixelMap iconPiexMap = getPixelMapFormFilePath(context, iconFilePath);
        PixelMap pixelMap = PicUtils.createReflectionImageWithOrigin(iconPiexMap);
        Assert.assertTrue(pixelMap.getImageInfo().size.width > 0);
    }

    @Test
    public void testSpilt() {
        Context context = AbilityDelegatorRegistry.getAbilityDelegator().getAppContext();
        PixelMap iconPiexMap = getPixelMapFormFilePath(context, iconFilePath);
        List<PicUtils.ImagePiece> list = PicUtils.spilt(context, iconPiexMap);
        Assert.assertTrue(list.size() > 0);
    }

    @Test
    public void testCreateBitmap() {
        Context context = AbilityDelegatorRegistry.getAbilityDelegator().getAppContext();
        ScrollView view = new ScrollView(context);
        view.setWidth(500);
        view.setHeight(1000);
        PixelMap pixelMap = PicUtils.createBitmap(context,view);
        Assert.assertNull(pixelMap);
    }

    @Test
    public void testRectBitmapToCircle() {
        Context context = AbilityDelegatorRegistry.getAbilityDelegator().getAppContext();
        PixelMap iconPiexMap = getPixelMapFormFilePath(context, iconFilePath);
        PixelMap pixelMap = PicUtils.rectBitmapToCircle(iconPiexMap);
        Assert.assertTrue(pixelMap.getImageInfo().size.width > 0);
    }


    @Test
    public void testGetMaskBitmap() {
        PixelMap pixelMap = PicUtils.getMaskBitmap(200,500);
        Assert.assertTrue(pixelMap.getImageInfo().size.width > 0);
    }

    private PixelMap getPixelMapFormFilePath(Context context, String filename) {
        PixelMap s_bitmap = null;
        RawFileEntry rawFileEntry = context.getResourceManager().getRawFileEntry(filename);
        try {
            Resource rs = rawFileEntry.openRawFile();
            int length = rs.available();
            byte[] buffer = new byte[length];
            rs.read(buffer, 0, length);
            ImageSource.SourceOptions srcOpts = new ImageSource.SourceOptions();
            srcOpts.formatHint = "image/png";

            ImageSource imageSourceNoOptions = ImageSource.create(buffer, srcOpts);
            ImageSource.DecodingOptions decodingOptions = new ImageSource.DecodingOptions();
            decodingOptions.desiredPixelFormat = PixelFormat.ARGB_8888;
            s_bitmap = imageSourceNoOptions.createPixelmap(decodingOptions);
            imageSourceNoOptions.release();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s_bitmap;
    }
}